package testcase.app;

import java.io.IOException;

import javax.xml.parsers.ParserConfigurationException;

import org.json.JSONException;
import org.json.JSONObject;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import org.xml.sax.SAXException;

import api.app.UserInfo;
import common.Host;
import common.TestData;

/**
 * 帮忙医APP体检预约获取用户信息接口UserInfo测试用例
 * @author gaowen
 *
 */
public class TestUserInfo
{	
	/**
	 * 帮忙医APP团检预约获取用户信息接口UserInfo测试环境数据驱动
	 * @return 测试数据
	 * @throws IOException
	 * @throws ParserConfigurationException
	 * @throws SAXException
	 */
	@DataProvider(name = "Test_Team")
	public static String[][] testUserInfoDataProvider_Team() throws IOException, ParserConfigurationException, SAXException
	{
		Host.setup("Test");
		TestData td=new TestData("app/TestUserInfo.xml");
		return td.getParam("Team");
	}
	
	/**
	 * 帮忙医APP团检预约获取用户信息接口UserInfo生产环境数据驱动
	 * @return 测试数据
	 * @throws IOException
	 * @throws ParserConfigurationException
	 * @throws SAXException
	 */
	@DataProvider(name = "Prod_Team")
	public static String[][] prodUserInfoDataProvider_Team() throws IOException, ParserConfigurationException, SAXException
	{
		Host.setup("Prod");
		TestData td=new TestData("app/TestUserInfo.xml");
		return td.getParam("Team");
	}
	
	
	/**
	 * 帮忙医APP个检预约获取用户信息接口UserInfo测试环境数据驱动
	 * @return 测试数据
	 * @throws IOException
	 * @throws ParserConfigurationException
	 * @throws SAXException
	 */
	@DataProvider(name = "Test_Person")
	public static String[][] testUserInfoDataProvider_Person() throws IOException, ParserConfigurationException, SAXException
	{
		Host.setup("Test");
		TestData td=new TestData("app/TestUserInfo.xml");
		return td.getParam("Person");
	}
	
	/**
	 * 帮忙医APP个检预约获取用户信息接口UserInfo生产环境数据驱动
	 * @return 测试数据
	 * @throws IOException
	 * @throws ParserConfigurationException
	 * @throws SAXException
	 */
	@DataProvider(name = "Prod_Person")
	public static String[][] prodUserInfoDataProvider_Person() throws IOException, ParserConfigurationException, SAXException
	{
		Host.setup("Prod");
		TestData td=new TestData("app/TestUserInfo.xml");
		return td.getParam("Person");
	}
	
	/**
	 * 帮忙医APP团检预约获取用户信息接口UserInfo测试环境测试用例
	 * @param casename 用例名称
	 * @param token	用户token
	 * @param resvPid 用户resvpid
	 */
	@Test(dataProvider="Test_Team",groups="Test")
	public void testUserInfoTestCase_Team(String casename,String token,String resvpid) throws IOException, JSONException
	{
		//调用接口并将返回数据转换成json格式
		String result=UserInfo.get(token, resvpid);
		JSONObject jsonResult;
		if (result==null || result.isEmpty())
		{
			jsonResult = null;
		}
		else
		{
			jsonResult = new JSONObject(result);
		}
		
		//设置检查点
		String checkpoint;
		switch (casename)
		{
			case "OwnResvpid":
				checkpoint=jsonResult.getJSONObject("Data").getString("Mobilephone");
				Assert.assertEquals(checkpoint,"15000000004");
				break;
			case "OtherResvpid":
				checkpoint=jsonResult.getJSONObject("Data").getString("Mobilephone");
				Assert.assertEquals(checkpoint,"15000000003");
				break;
			case "TokenError":
				Assert.assertNull(result);
				break;
			case "TokenNull":
				Assert.assertNull(result);
				break;
			case "ResvpidError":
				Assert.assertNull(result);
				break;
			case "ResvpidNull":
				Assert.assertNull(result);
				break;
			default:
				break;
		}
	}
	
	/**
	 * 帮忙医APP团检预约获取用户信息接口UserInfo生产环境测试用例
	 * @param casename 用例名称
	 * @param token	用户token
	 * @param resvPid 用户resvpid
	 */
	@Test(dataProvider="Prod_Team",groups="Prod")
	public void prodUserInfoTestCase_Team(String casename,String token,String resvpid) throws IOException, JSONException
	{
		//调用接口并将返回数据转换成json格式
		String result=UserInfo.get(token, resvpid);
		JSONObject jsonResult;
		if (result==null || result.isEmpty())
		{
			jsonResult = null;
		}
		else
		{
			jsonResult = new JSONObject(result);
		}
		
		//设置检查点
		String checkpoint;
		switch (casename)
		{
			case "OwnResvpid":
				checkpoint=jsonResult.getJSONObject("Data").getString("Mobilephone");
				Assert.assertEquals(checkpoint,"15921011131");
				break;
			case "OtherResvpid":
				checkpoint=jsonResult.getJSONObject("Data").getString("Mobilephone");
				Assert.assertEquals(checkpoint,"18795901919");
				break;
			case "TokenError":
				Assert.assertNull(result);
				break;
			case "TokenNull":
				Assert.assertNull(result);
				break;
			case "ResvpidError":
				Assert.assertNull(result);
				break;
			case "ResvpidNull":
				Assert.assertNull(result);
				break;
			default:
				break;
		}
	}
	
	/**
	 * 帮忙医APP个检预约获取用户信息接口UserInfo测试环境测试用例
	 * @param casename 用例名称
	 * @param token 用户token
	 * @throws IOException
	 * @throws JSONException
	 */
	@Test(dataProvider="Test_Person",groups="Test")
	public void testUserInfoTestCase_Person(String casename,String token) throws IOException, JSONException
	{
		//调用接口并将返回数据转换成json格式
		String result=UserInfo.get(token);
		JSONObject jsonResult;
		if (result==null || result.isEmpty())
		{
			jsonResult = null;
		}
		else
		{
			jsonResult = new JSONObject(result);
		}
				
		//设置检查点
		String checkpoint;
		switch (casename)
		{
			case "GetSuccess":
				checkpoint=jsonResult.getJSONObject("Data").getString("Mobilephone");
				Assert.assertEquals(checkpoint,"15000000004");
				break;
			case "TokenError":
				Assert.assertNull(result);
				break;
			case "TokenNull":
				Assert.assertNull(result);
				break;
			default:
				break;
		}
	}
	
	/**
	 * 帮忙医APP个检预约获取用户信息接口UserInfo生产环境测试用例
	 * @param casename 用例名称
	 * @param token 用户token
	 * @throws IOException
	 * @throws JSONException
	 */
	@Test(dataProvider="Prod_Person",groups="Prod")
	public void prodUserInfoTestCase_Person(String casename,String token) throws IOException, JSONException
	{
		//调用接口并将返回数据转换成json格式
		String result=UserInfo.get(token);
		JSONObject jsonResult;
		if (result==null || result.isEmpty())
		{
			jsonResult = null;
		}
		else
		{
			jsonResult = new JSONObject(result);
		}
				
		//设置检查点
		String checkpoint;
		switch (casename)
		{
			case "GetSuccess":
				checkpoint=jsonResult.getJSONObject("Data").getString("Mobilephone");
				Assert.assertEquals(checkpoint,"15921011131");
				break;
			case "TokenError":
				Assert.assertNull(result);
				break;
			case "TokenNull":
				Assert.assertNull(result);
				break;
			default:
				break;
		}
	}
}
